package me.zhengjie;

import me.zhengjie.modules.hanzi.service.CharacterService;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.core.io.ClassPathResource;
import org.springframework.transaction.annotation.EnableTransactionManagement;

import jakarta.annotation.PostConstruct;
import java.io.BufferedReader;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * 汉字学习应用启动类
 */
@EnableTransactionManagement
@SpringBootApplication
@EnableConfigurationProperties
@MapperScan("me.zhengjie.modules.*.mapper")
public class HanziApplication {

    @Autowired
    private CharacterService characterService;

    public static void main(String[] args) {
        SpringApplication.run(HanziApplication.class, args);
    }

    @PostConstruct
    public void init() {
        String filePath = "d:\\uniapp\\learn-java\\src\\main\\resources\\hanzi\\hanzi.txt";
        Map<Character, Integer> hanziCount = new HashMap<>();

        try (BufferedReader br = new BufferedReader(new FileReader(filePath))) {
            String line;
            while ((line = br.readLine()) != null) {
                if (!line.isEmpty()) {
                    char firstChar = line.charAt(0); // 获取每行第一个字符
                    hanziCount.put(firstChar, hanziCount.getOrDefault(firstChar, 0) + 1);
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        // 打印重复的汉字
        System.out.println("重复的汉字有:");
        for (Map.Entry<Character, Integer> entry : hanziCount.entrySet()) {
            if (entry.getValue() > 1) {
                System.out.println(entry.getKey() + " - 出现次数: " + entry.getValue());
            }
        }
        characterService.initCharacter();
    }
} 